package win.ganbo.softwareFive.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import win.ganbo.softwareFive.domain.LoginToken;
import win.ganbo.softwareFive.service.ILoginTokenService;
import win.ganbo.softwareFive.utils.ConstantConfig;

@Controller
@RequestMapping("/sys")
public class LoginController extends BaseController {

    @Autowired
    private ILoginTokenService loginTokenService;

    @RequestMapping("/login")
    public String login(String username, String password, HttpServletRequest request) {
        if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
            List<?> list = loginTokenService.findByHql(
                "FROM LoginToken o where o.username = ? and password = ?  ", username, password);
            if (list != null && list.size() == 1) {
                LoginToken token = (LoginToken) list.get(0);
                request.getSession().setAttribute(ConstantConfig.USER_IN_SESSION, token);

                System.out.println("登陆成功。。。。");
                token.setLogin(true);//设置为已经登录过
                loginTokenService.update(token);
                return "redirect:/user/listUI";
            }
            {
                System.out.println("用户名或者密码错误======");
            }
        }
        return "redirect:loginUI";
    }

    @RequestMapping("/loginUI")
    public String loginUI() {
        return "login";
    }

}
